package com.adil.base.lambda;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @description: lambda学习联系
 * @author: Adil
 * @date: 2024/4/7
 */
public class LambdaBase {
    public static void main(String[] args) {
        List<String> list = Arrays.asList("apple", "banana", "orange");
        LambdaBase lambdaBase = new LambdaBase();
//        lambdaBase.forEach(list);
//        lambdaBase.sort(list);
//        lambdaBase.compare(list);
//        lambdaBase.startsWith(list);
//        lambdaBase.reduce();
//        lambdaBase.grouping(list);
        lambdaBase.myInterfaceMethod();
    }

    /**
     * lambda forEach基础循环
     */
    public void forEach(List list) {
        for (Object name : list) {
            System.out.println(name);
        }
        /**
         * lambda 简化forEach
         */
        list.forEach(name -> {
            System.out.println(name);
        });
    }

    /**
     * lambda 排序
     *
     * @param list
     */
    public void sort(List list) {
        list = Arrays.asList("banana", "orange", "apple");
        // collection 合集、收集
        Collections.sort(list);
        list.forEach(name -> {
            System.out.println(name);
        });
        // 译：comparator 比较器
        Collections.sort(list, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                // 译： compare 比较
                return o1.compareTo(o2);
            }
        });
    }

    /**
     * lambda 对比
     * TODO 暂时不理解干啥用的
     *
     * @param list
     */
    public void compare(List<String> list) {
        Collections.sort(list, (s1, s2) -> {
            return s1.compareTo(s2);
        });
    }

    /**
     * 使用Lambda表达式进行过滤
     * startsWith 检查字符串头部是否支持指定的字符串
     * 参数 prefix：要检查的前缀字符串。
     * 返回值：如果字符串以指定的前缀开始，则返回 true；否则返回 false。
     *
     * @param list
     */
    public void startsWith(List<String> list) {
        List filteredList = new ArrayList();
        for (String fruit : list) {
            if (fruit.startsWith("a")) {
                // 表示从下标的位置开始检查,是否支持
                // if (list.get(i).startsWith("a",2)) {
                filteredList.add(fruit);
            }
        }
        // lambda 表达式
        List filteredListLambda = list.stream().filter(fruit ->
                fruit.startsWith("a")
        ).collect(Collectors.toList());
        List<Integer> collect = list.stream().map(fruit -> fruit.length()).collect(Collectors.toList());
        System.out.println("collect: " + collect);
        filteredList.forEach(name -> System.out.println(name));
        filteredListLambda.forEach(fruit -> System.out.println(fruit));
    }

    /**
     * lambda 归约
     */
    public void reduce() {
        //译 reduce 减少 降低 简化
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
        int sum = 0;
        for (int index : list) {
            sum = sum + index;
        }
        System.out.println(sum);
        // lambda
        int sums = list.stream().reduce(0, (a, b) -> a + b);
        System.out.println(sums);
    }

    public void grouping(List<String> list) {
        Map<Integer, List> map = new HashMap<>();
        for (String fruit: list) {
            // 获取长度
            int length = fruit.length();
            if (!map.containsKey(length)) {
                map.put(length, new ArrayList());
            }
            map.get(length).add(fruit);
        }
        System.out.println(map);

    }

    public void  myInterfaceMethod() {
        MyInterface myInterface = new MyInterface() {
            @Override
            public void doSomething(String input) {
                // 方法体中编辑业务逻辑
                System.out.println(input);
                System.out.println(1+1);
            }
        };
        myInterface.doSomething("调用了一次函数");
    }
}
